package com.wangyb.wework.utils;

import cn.hutool.crypto.SecureUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Random;

/**
 * @Author: wangyb
 * @Date: 2021/6/28 5:17 下午
 */
public class FileUtils {
	private static final Random RANDOM = new Random();

	private static final String EMPLOYEE_AVATAR = "mochat/test/avatar";
	private static final String EMPLOYEE_THUMB_AVATAR = "mochat/employee/thumb_avatar";
	private static final String EMPLOYEE_QR_CODE = "mochat/employee/qr_code";

	private static final String CONTACT_CONTACT_WAY_QR_CODE = "ContactWayClient/QrCode";

	private static final String CONTACT_AVATAR = "contact/avatar";

	private static final String FILE_PNG = ".png";


	public static String getFileName(String module, String suffix) {
		return module + getRandomString() + suffix;
	}

	public static String getFileNameOfEmpAvatar() {
		return EMPLOYEE_AVATAR + getRandomString() + FILE_PNG;
	}

	public static String getFileNameOfEmpThumbAvatar() {
		return EMPLOYEE_THUMB_AVATAR + getRandomString() + FILE_PNG;
	}

	public static String getFileNameOfEmpQrCode() {
		return EMPLOYEE_QR_CODE + getRandomString() + FILE_PNG;
	}

	/**
	 * 客户自动拉群二维码
	 */
	public static String getFileNameOfContactWayQrCode() {
		return CONTACT_CONTACT_WAY_QR_CODE + getRandomString() + FILE_PNG;
	}

	public static boolean saveFileByInput(File file, InputStream inputStream) {
		try {
			file.getParentFile().mkdirs();
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			int offset;
			while ((offset = inputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, offset);
			}
			inputStream.close();
			fileOutputStream.flush();
			fileOutputStream.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * @description 获取客户头像存储阿里云路径
	 * @author zhaojinjian
	 * @createTime 2020/12/23 15:48
	 * @param tencentAvatarUrl
	 */
	public static String getContactAvatarPath(final Long corpId,final String tencentAvatarUrl) {
		String newPath = CONTACT_AVATAR + "/"+ corpId+"/"+ SecureUtil.md5(tencentAvatarUrl) + FILE_PNG;
		return newPath;
	}

	public static String getRandomString() {
		return getRandomString(6);
	}
	public static String getRandomString(int length) {
		StringBuilder builder = new StringBuilder();
		builder.append(System.currentTimeMillis() * 10);
		for (int i = 0; i < length; i++) {
			builder.append(RANDOM.nextInt(10));
		}
		return builder.toString();
	}
}
